<?php





function generate_female_name() {
            $ci =& get_instance();
            
            $query = $ci->doctrine->em->createQuery("select max(u.id) from ORM\Dynasties2\Femalenames u");
            $result = $query->getSingleResult();
            //echo $result2[1];
            $highval = $result[1];
            $random_name = rand(1,$highval);
            //echo $random_name;
            $name = $ci->doctrine->em->find('ORM\Dynasties2\Femalenames', $random_name);
            return $name->getName();
                    
        }  //// END OF FUNCTION generate_female_name
//////////////////////////////////////////////////////////////////////////////////////////////////////////
        
function generate_male_name() {
        $ci =& get_instance();
        
        //$query = $this->doctrine->em->createQuery("select max(u.id) from ORM\Dynasties2\Malenames u");
        $query = $ci->doctrine->em->createQuery("select max(u.id) from ORM\Dynasties2\Malenames u");
             $result = $query->getSingleResult();
             //echo $result2[1];
             $highval = $result[1];
             $random_name = rand(1,$highval);
             //echo $random_name;
          //$name = $this->doctrine->em->find('ORM\Dynasties2\Malenames', $random_name);
          $name = $ci->doctrine->em->find('ORM\Dynasties2\Malenames', $random_name);
             return $name->getName();
            
            
        }//// END OF FUNCTION generate_male_name
//////////////////////////////////////////////////////////////////////////////////////////////////////////
        
        
        function get_a_rulers_regions($data) {
            // a test helper function meant to return the region IDs that are directly controlled by a ruler
            // assume that it is passed a value, preferably the ruler's character ID
            $ci =& get_instance();
            
            //$ruler_character_id = '14281';
            
            
            $query =$ci->doctrine->em->createQuery("
                SELECT u 
                FROM ORM\Dynasties2\Titles u
                    WHERE u.characterLink = $data
                ");
            
            $result = $query->getResult();
            
            $my_regions = array();
            foreach ($result as $row) {
                $my_regions[] = $row->getRegionLink()->getId();
                
            }
            return $my_regions;
            //print_r($my_regions);
            
            
        } //// END OF FUNCTION get_a_rulers_regions
//////////////////////////////////////////////////////////////////////////////////////////////////////////
        
        function tendten() {
            
            $number = '10';
            $results = '0';
            while ($number > '0') {
                $dice = mt_rand(1,10);
                //echo $dice . "</br>";
                $results = $dice + $results;
                  
                $number--;
            }
            //echo "<br>* $results * </br>";
            return $results;
                    
        }/// end public function tendten()
////////////////////////////////////////////////////////////////////////////////////////////////
        
        function fivedtwenty() {
            $number = '5';
            $results = '0';
            while ($number > '0') {
                $dice = mt_rand(1,20);
                //echo $dice . "</br>";
                $results = $dice + $results;
                  
                $number--;
            }
            //echo "<br>* $results * </br>";
            return $results;
            
            
                    
        }/// end public function fivedtwenty()
////////////////////////////////////////////////////////////////////////////////////////////////
        
        
        
        function call_levies($which_character) {
            $ci =& get_instance();
            
            $turndata = $ci->doctrine->em->find('ORM\Dynasties2\Currentturn', '1');
             $current_turn = $turndata->getTurnNumber();  
            // this needs to be moved out of turn.php
            // for the time being, this function should create an army, callup levies from the character's region, and attach those soldiers
            //    plus the character to the army
            
            $logmsg = new ORM\Dynasties2\Gamelog;
            $logmsg -> setOnTurn($current_turn);
            $logmsg -> setText("Call Levies was initiated in the helper.");
            $logmsg -> setTimeStamp(new DateTime());
            $logmsg -> setMsgType('3');
            $ci->doctrine->em->persist($logmsg);
            $ci->doctrine->em->flush();
            
            $existing_army = $ci->doctrine->em->getRepository('ORM\Dynasties2\Armies')->findOneBy(array('controlledBy' => $which_character));
            if (is_null($existing_army)) {
                $logmsg = new ORM\Dynasties2\Gamelog;
                $logmsg -> setOnTurn($current_turn);
                $logmsg -> setText("Call Levies executes code after querying existing_armies.");
                $logmsg -> setTimeStamp(new DateTime());
                $logmsg -> setMsgType('3');
                $ci->doctrine->em->persist($logmsg);
                $ci->doctrine->em->flush();
                
                $character_data = $ci->doctrine->em->find('ORM\Dynasties2\Characters', $which_character);

                // create army and components
                $new_army = new ORM\Dynasties2\Armies;
        
                $new_army -> setControlledBy($which_character);

                $ci->doctrine->em->persist($new_army);
                $ci->doctrine->em->flush();
                                    
                $new_armyForce = new ORM\Dynasties2\Armyforces;
        
                $new_armyForce -> setattachedNoble($which_character);
                $new_armyForce -> setLeadership($character_data->getLeadership());
                $new_armyForce -> setArmyLink($new_army);

                $ci->doctrine->em->persist($new_armyForce);
                
                $demense = $ci->doctrine->em->getRepository('ORM\Dynasties2\Titles')->findBy(array('characterLink' => $which_character));
                foreach ($demense as $row) {
                    
                    //echo "<br>Returned one row from demense check</br>";
                    
                    $soldiers = $row->getRegionLink()->getAvailSoldiers();
                    $row->getRegionLink()->setAvailSoldiers('0');
                    if ($soldiers < '1') {
                        echo "<br><b>Error, a query in call_levies has returned 0 soldiers!!!</b></br>";
                        $logmsg = new ORM\Dynasties2\Gamelog;
                        $logmsg -> setOnTurn($current_turn);
                        $logmsg -> setText("ERROR - A query in call_levies returned 0 soldiers, region " . $row->getRegionLink()->getId());
                        $logmsg -> setTimeStamp(new DateTime());
                        $logmsg -> setMsgType('3');
                        $ci->doctrine->em->persist($logmsg);
                    }
                    $from = $row->getRegionLink()->getId();
                    
                    $new_armyForce = new ORM\Dynasties2\Armyforces;
        
                    $new_armyForce -> setSoldiers($soldiers);
                    $new_armyForce -> setSoldiersFrom($from);
                    $new_armyForce -> setArmyLink($new_army);

                    $ci->doctrine->em->persist($new_armyForce);
                    
                }
                
                $logmsg = new ORM\Dynasties2\Gamelog;
                $logmsg -> setOnTurn($current_turn);
                $logmsg -> setText("Call Levies was completed in the helper.");
                $logmsg -> setTimeStamp(new DateTime());
                $logmsg -> setMsgType('3');
                $ci->doctrine->em->persist($logmsg);
            
                $ci->doctrine->em->flush();
                
            } else {
                // do nothing
                $logmsg = new ORM\Dynasties2\Gamelog;
                $logmsg -> setOnTurn($current_turn);
                $logmsg -> setText("Call Levies did nothing in the helper.");
                $logmsg -> setTimeStamp(new DateTime());
                $logmsg -> setMsgType('3');
                $ci->doctrine->em->persist($logmsg);
                $ci->doctrine->em->flush();
            }
            
            
            
        }   /// end public function call_levies()
////////////////////////////////////////////////////////////////////////////////////////////////
        
        



/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
?>
